﻿using UnityEngine;
using System.Collections;

public class FileCover : MonoBehaviour
{


    GameObject FileCoverPref;
    UnitData CurSelUnit;

    TweenPosition[] tweenerPos;
    [HideInInspector]
    public bool bDestroy;

    bool bJizz;
    public bool bInit;
    public bool bLeft;

    void Awake()
    {

        transform.localPosition = new Vector3(-72, 16.5f);
        transform.localScale = new Vector3(1.1f, 1.3f);

        CurSelUnit = new UnitData();

        CurSelUnit = GameObject.Find("ShopWdw(Clone)").GetComponent<HireWindow>().m_HireList[GameObject.Find("RightWdw").GetComponent<RightWdwMgr>().CurApplicationNum];
        //GetComponent<UISprite> ().spriteName = string.Format("FileCover{0}", (int)CurSelUnit.ClassStatus.enClass );

        transform.FindChild("SealImg").localPosition = new Vector3(9.6f, 18.5f);
        transform.FindChild("ClassNameLabel").GetComponent<UILabel>().text = CurSelUnit.ClassStatus.AssnStatus.strAssnName;
        transform.FindChild("ClassInformLabel").GetComponent<UILabel>().text = CurSelUnit.ClassStatus.AssnStatus.strAssnExplain;
        transform.FindChild("ClassPriceLabel").GetComponent<UILabel>().text = string.Format("Training Fee $ {0}", CurSelUnit.ClassStatus.AssnStatus.iAssnTrainPrice);
        switch (CurSelUnit.ClassStatus.enClass)
        {
            case CLASS_TYPE.TECHNICIAN:
                transform.FindChild("SealImg").GetComponent<UISprite>().spriteName = string.Format("Technician{0}", (int)CurSelUnit.ClassStatus.AssnStatus.enAssnType);
                break;

            case CLASS_TYPE.POLICE:
                transform.FindChild("SealImg").GetComponent<UISprite>().spriteName = string.Format("Police{0}", (int)CurSelUnit.ClassStatus.AssnStatus.enAssnType);
                break;
        }


        bDestroy = false;

        GetComponent<UISprite>().depth = 72;
        transform.GetChild(0).GetComponent<UISprite>().depth = GetComponent<UISprite>().depth + 1;
        transform.GetChild(1).GetComponent<UILabel>().depth = GetComponent<UISprite>().depth + 1;
        transform.GetChild(2).GetComponent<UILabel>().depth = GetComponent<UISprite>().depth + 1;
        transform.GetChild(3).GetComponent<UILabel>().depth = GetComponent<UISprite>().depth + 1;
        transform.GetChild(4).GetComponent<UISprite>().depth = GetComponent<UISprite>().depth + 1;

        tweenerPos = GetComponents<TweenPosition>();

        bInit = true;
        bJizz = false;
        bLeft = true;
    }


    void Start()
    {
        FileCoverPref = Resources.Load("Prefaps/UI/StaticUI/FileCover/FileCover") as GameObject;
        transform.localScale = Vector3.one;

        if (bLeft)
        {
            tweenerPos[0].Play(true);
            bJizz = false;
        }
        else
        {
            tweenerPos[4].Play(true);
            bJizz = false;
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (!bJizz && transform.localPosition.x >= 1.3 && bLeft)
        {
            bJizz = true;
            StartJizz();
        }

        if (!bJizz && transform.localPosition.x <= 5.3 && !bLeft)
        {
            bJizz = true;
            StartJizz();
        }

        if (bDestroy && transform.localPosition.x <= -71)
        {
            Destroy(gameObject);
        }
    }

    void StartJizz()
    {
        tweenerPos[1].Play(true);
        EventDelegate.Add(tweenerPos[1].onFinished, EndJizzing);
    }

    void EndJizzing()
    {
        transform.localPosition = new Vector3(3.3f, 16.5f);
        GameObject.Find("ClassArrows(Clone)").transform.GetChild(0).GetComponent<ClassArrow>().ButtonStatus(true);
        GameObject.Find("ClassArrows(Clone)").transform.GetChild(1).GetComponent<ClassArrow>().ButtonStatus(true);
        GameObject.Find("HireApply(Clone)").GetComponent<HireApply>().ButtonStatus(true);

        if (bInit)
        {
            Destroy(transform.parent.FindChild("HireApply(Clone)").FindChild("Check(Clone)").gameObject);
            GameObject.Find("HireApply(Clone)").GetComponent<HireApply>().bFileCoverOn = true;
            GameObject.Find("HireApply(Clone)").transform.FindChild("Label").GetComponent<UILabel>().text = "CONFIRM";
        }
        //StartCoroutine (SwipeTopTimer ());
    }

    public void SwipeTopTimer()
    { // Hire!!
        for (int i = 0; i < GameObject.Find("DataLogic").GetComponent<DataManager>().HireList.Count; ++i)
        {
            if (GameObject.Find("ShopWdw(Clone)").transform.GetChild(i + 1).GetComponent<ApplicationBtn>().CurAppNum
               == GameObject.Find("RightWdw").GetComponent<RightWdwMgr>().CurApplicationNum)
                GameObject.Find("ShopWdw(Clone)").transform.GetChild(i + 1).GetComponent<ApplicationBtn>().bSwipeToTop = true;
        }
        EventDelegate.Add(tweenerPos[2].onFinished, DestroyThis);
        tweenerPos[2].Play(true);

        Destroy(GameObject.Find("ClassArrows(Clone)").gameObject);

        Destroy(transform.parent.FindChild("HireApply(Clone)").FindChild("Check(Clone)").gameObject);
        GameObject.Find("RightWdw").GetComponent<RightWdwMgr>().CurApplicationNum = 6;
    }

    public void JustDestroy()
    {
        Destroy(gameObject);
    }

    void DestroyThis()
    {
        Destroy(gameObject);
        GameObject.Find("RightWdw").GetComponent<RightWdwMgr>().bSwipeToTop = false;
    }

    public void RightArrow()
    {
        (Instantiate(FileCoverPref) as GameObject).transform.parent = transform.parent;
        transform.parent.GetChild(transform.parent.childCount - 1).GetComponent<FileCover>().bInit = false;
        transform.parent.GetChild(transform.parent.childCount - 1).GetComponent<FileCover>().bLeft = true;

        EventDelegate.Add(tweenerPos[3].onFinished, DestroyThis);
        tweenerPos[3].Play(true);
    }

    public void LeftArrow()
    {
        (Instantiate(FileCoverPref) as GameObject).transform.parent = transform.parent;
        transform.parent.GetChild(transform.parent.childCount - 1).GetComponent<FileCover>().bInit = false;
        transform.parent.GetChild(transform.parent.childCount - 1).GetComponent<FileCover>().bLeft = false;

        EventDelegate.Add(tweenerPos[5].onFinished, DestroyThis);
        tweenerPos[5].Play(true);
    }
}
